﻿@model Proggy.Web.Cms.ViewModels.SiteMapViewModel
@{
    // get rendering
    var rend = Model.CurrentlyRenderingNode;

    // check live
    var liveTestOk = (Model.UseLiveLinksAndNode && rend.IsPublished) || !Model.UseLiveLinksAndNode;

    //
    if (liveTestOk)
    {

        // get draft
        var draft = rend.DraftNode;

        // get isdraft
        var useDraft = draft != null;

        // get name
        var name = !useDraft ? rend.Name : draft.Name;

        // get open children bool
        var open = Model.Opens.Contains(rend);

        // get selected
        var slct = Model.Selecteds.Contains(rend);

        // get text
        var linkQuery = open ? Model.GetCloseQueryStringForNode(rend) : Model.GetOpenQueryStringForNode(rend);
        var linkClass = open ? "proggy-sm-link-open" : "proggy-sm-link-closed";
        var linkText = open ? "Close" : "Open";

        // get link
        var link = Model.UseLiveLinksAndNode ? Model.GetLiveLink(rend) : Html.ActionLink(name, "Preview", new { controller = "Content", area = "Proggy", id = rend.PublishableNodeId }, new
        {
            @class = "proggy-sm-preview-link proggy-sm-preview-link-selected" + slct,
            data_nodeid = rend.PublishableNodeId
        });

        // em
        if (useDraft && Model.IncludeContentMenu)
        {

            link = MvcHtmlString.Create("<em>" + HttpUtility.HtmlEncode(link) + "</em>");

        }

        // child bool
        var childNodes = Model.AllNodes.Where(x => x.ParentNode != null && x.ParentNode.PublishableNodeId == rend.PublishableNodeId).ToList();        
        var parent = rend.ParentNode;
        var siblings = parent != null ? Model.AllNodes.Where(x => x.ParentNode != null && x.ParentNode.PublishableNodeId == parent.PublishableNodeId && x.PublishableNodeId != rend.PublishableNodeId).ToList() : null;
        
        // check
        if (Model.UseLiveLinksAndNode)
        {
            parent = parent.IsPublished ? parent : null;
            childNodes = childNodes.Where(x => x.IsPublished).ToList();
            siblings = childNodes.Where(x => x.IsPublished).ToList();
        }

        var hasParent = parent != null;
        var childBool = childNodes.Count > 0;
        var hasChildren = childBool;
        var childCount = childNodes.Count;
        

        // siblings
        var hasSiblings = siblings != null;
        
    
    <li class="proggy-sm-selected@(slct)" style="background-image: url('@Url.Content(Model.GetIconSrcForCurrentNode())');">
        @if (rend.IsRoot)
        { 
            <span class="proggy-sm-link proggy-sm-link-open"><span class="hidden">@linkText</span></span>
        }
        else
        {
            if (childBool)
            {
            <a class="proggy-sm-link @(linkClass)" href="@(Request.Path + linkQuery)"><span class="hidden">@linkText</span>
            </a>
            }
            else
            { 
            <span class="proggy-sm-link">&nbsp;</span>
            }
        }
        @link &nbsp;&nbsp;&nbsp;&nbsp;<a href="#">&nbsp;</a>
        @if (Model.IncludeContentMenu)
        { 
            <span class="tree-item-menu" data-nodeid="@rend.PublishableNodeId"><span class="strong">
                Options: </span>
                @Html.ActionLink("Create new here", "Create", new { parentNodeId = rend.PublishableNodeId })
                |
                @Html.ActionLink("Edit", "Edit", new { id = rend.PublishableNodeId })
                @if (useDraft)
                {
                    @: | @Html.ActionLink("Publish", "Publish", new { id = rend.PublishableNodeId })
                    @: | @Html.ActionLink("Preview", "Preview", new { id = rend.PublishableNodeId }, new { target = "_blank", @class = "proggy-content-preview" })
                                                                                                }
                @if (rend.IsPublished)
                {
                    @: | @Html.ActionLink("View published", "HandleUrl", new
                    {
                        controller = rend.TypeAlias,
                        //url = model.UrlPortion,
                        nodeId = rend.PublishableNodeId,
                        asJson = false,
                        area = ""
                    }, new { target = "_blank" })
                                                                                                }
                @if (useDraft && rend.IsPublished)
                {
                    @: | @Html.ActionLink("Revert", "Revert", new { id = rend.PublishableNodeId })
                                                                                                }
                &nbsp;| @Html.ActionLink("Trash", "Trash", new { id = rend.PublishableNodeId })
                @if (childCount > 1)
                { 
                    @: | @Html.ActionLink("Sort children", "SortChildren", new { id = rend.PublishableNodeId })
                                                                                }
                @if (!rend.IsRoot)
                { 
                    @: | @Html.ActionLink("Move", "Move", new { id = rend.PublishableNodeId })
                                                                                }
            </span>
        }
        @if (open && hasChildren)
        {
            <ul>
                @{
            // get data
            var data = childNodes;

            // iterate
            foreach (var child in data)
            {
                // set rend
                Model.CurrentlyRenderingNode = child;

                // render
                Html.RenderPartial("_SiteMapItem", Model);

            }
                }
            </ul>
        }
    </li>
    }
    }
